<?php

class MenuAction extends CommonAction{
	
	private $menu ;
	
	public function __construct(){
		parent::__construct();
		$this->menu = D('Menu');
	}
	
	function _before_add(){
		
		$top = $this->menu->getTopMenu();
		foreach($top as $k=>$v){
			$top[$k]['sub'] = $this->menu->get2ndMenu($v['id']);
		}
		$this->assign('top',$top);
	}

	function _after_add(){
		S('urls','');
	}
	
	public function add(){
		$this->display();
	}
	
	public function insert(){
		
		$this->menu->create();
		$mid = $this->menu->add();
		if($mid){
			echo 1;
		}else{
			echo 0;
		}
	}
	public function update(){
		$data = $_REQUEST;
		$this->menu->create($data);
		$mid = $this->menu->save();
		if($mid){
			echo 1;
		}else{
			echo 0;
		}
	}
	
	public function index(){
		
		$menus = $this->menu->order('sort desc')->select();
		$this->assign('menus',$menus);
		
		$top = $this->menu->getTopMenu();
		foreach($top as $k=>$v){
			$top[$k]['sub'] = $this->menu->get2ndMenu($v['id']);
		}
		$this->assign('top',$top);
		
		
		//all menus
		$menu = $this->menu->getTopMenu();
		foreach($menu as $key=>$val){ //top
			$menu[$key]['sub'] = $this->menu->getSubMenu($val['id']);
		}
		$this->assign('menu',$menu);
		$this->display();
	}
	
	public function getMenuById(){
		$menuid = $_REQUEST['id'];
		$menu = $this->menu->getById($menuid);
		echo json_encode($menu);
	}
	
	public function delete(){
		$id = $_REQUEST['id'];
		$this->menu->where('pid='.$id)->delete();
		echo $this->menu->where('id='.$id)->delete()?1:0;
	}
	
}

?>